<?php

namespace app\common\business;

use \think\Request;
use \think\Session;
use app\common\mysql\Users as usersMysql;
use app\common\business\upload\Upload as uploadBn;

class Guess extends \app\common\business\AbstractModel {

    /**
     * 会员列表
     * page
     * count
     * need_total
     */
    public static function guesslist($params) {
        self::_checkPageAndCount($params);
        $where = self::_getWhere($params);
        $listdata = usersMysql::name('guess')
                ->where($where)
                ->limit($params['offset'], $params['count'])
                ->select();
        if (!empty($listdata)) {
            $listdata = self::_adapterList($listdata);
        }
        if (isset($params["need_total"]) && $params["need_total"] == 1) {
            return array("listdata" => $listdata, "total" => self::total($params));
        }
        return array("listdata" => $listdata);
    }

    /**
     * 获取Query条件
     *
     * @param array $params
     * @return array
     */
    protected static function _getQuery($params) {
        $query = array();
        if (isset($params["type"])) {
            $query['type'] = $params["type"];
        }
        if (isset($params["user_id"])) { //userid
            $query['user_id'] = $params["user_id"];
        }

        if (isset($params["guess_first"])) { //冠军
            $query['guess_first'] = $params["guess_first"];
        }

        if (isset($params["str_time"])) { //时间
            $query['str_time'] = $params["str_time"];
        }
        if (isset($params["end_time"])) { //时间
            $query['end_time'] = $params["end_time"];
        }

        return $query;
    }

    /**
     * 适配输出
     *
     * @params
     * @return array
     */
    protected static function _adapterList($result) {

        foreach ($result as $k => $val) {

            $result[$k]['nickname'] = $val['nickname'] ? $val['nickname'] : '球宝' . $val['user_id'];
        }
        return $result;
    }

    /**
     * 获取where条件
     *
     * @param array $params
     * @return array
     */
    protected static function _getWhere($params) {
        $where = array();
        if (isset($params["type"]) && !empty($params['type'])) { // 
            $where['type'] = $params["type"];
        }
        if (isset($params["user_id"]) && !empty($params['user_id'])) { // 
            $where['user_id'] = $params["user_id"];
        }

        if (isset($params["guess_first"]) && !empty($params['guess_first'])) { // 
            $where['guess_first'] = $params["guess_first"];
        }

        if (isset($params["str_time"]) && !empty($params['end_time'])) { // 
            if (isset($params['str_time'])) {
                $params['str_time'] = strtotime($params['str_time']);
            }
            if (isset($params['end_time'])) {
                $params['end_time'] = strtotime($params['end_time']);
            }
            $where['add_time'] = array('between', [$params['str_time'], $params['end_time']]);
        }
        return $where;
    }

    /**
     * 计算总数
     *
     * @param array $params
     * @return int
     */
    public static function total($params) {
        $where = self::_getWhere($params);
        $count = usersMysql::name('guess')
                ->where($where)
                ->count();
        return $count;
    }

    /**
     * 是记录记录
     * user_id 当前用户
     * guess_16
     */
    public static function addguess($post) {
        $user_id = $post['user_id'];
        $where = array('user_id' => $user_id);

        //用户ID是否存在
        $dataguess = usersMysql::name('guess')
                ->where($where)
                ->find();
        if ($dataguess) {
             $team = usersMysql::name('national_team')
                ->where(array('team_id'=>$dataguess['guess_first']))
                ->find();
             $dataguess['is_finish']=1;
			 $dataguess['team_name']=$team['team_name'];
			 $dataguess['team_logo']=$team['team_logo'];
             return $dataguess; 
        }
        $data = $post;
        $data['add_time'] = time();
        $userimg = isset($post['guess_img']) ? $post['guess_img'] : "";
        if ($data['guess_img']) {

            if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $data['guess_img'])) {

                $data['guess_img'] = uploadBn::uploadav($data['guess_img'])['dest_file_name'];
            }else{
                  exception('冠军推演图格式不正确', 10000003);
            }
        }
        $r = usersMysql::name('guess')->insertGetId($data);
        if ($r) {
			 $data['is_finish']=0;
            return $data;
        }
        exception('推演出冠军失败', 10000003);
    }

    /**
     * 冠军排行榜
     * page
     * count
     * need_total
     */
    public static function firstlist($params = array(), $user_id = '') {

        self::_checkPageAndCount($params);
        $listdata = usersMysql::name('national_team')
                ->field('team_name,team_logo,(total+base_count) as totals')
                ->order('totals desc')
                ->select();
        $total = usersMysql::name('national_team')
            ->sum(total);
        $base_count = usersMysql::name('national_team')
            ->sum(base_count);
        $totals=$total+$base_count;
        if (!empty($listdata)) {
            $listdata = self::_adapterfirstlist($listdata,$totals);
        }
        $totalsum= F('System')['teambase_count'];
        return array("listdata" => $listdata, 'total' => $total + $base_count+$totalsum);
        return array("listdata" => $listdata, 'total' => $total + $base_count);
    }

    /**
     * 计算总数
     *
     * @param array $params
     * @return int
     */
    public static function firstlisttotal($params) {
        $where = self::_getWhere($params);
        $count = usersMysql::name('guess')
                ->where($where)
                ->order('total desc')
                ->group('guess_first')
                ->count();
        return $count;
    }

    /**
     * 适配输出
     *
     * @params
     * @return array
     */
    protected static function _adapterfirstlist($result,$totals=0) {
        $totalsum= F('System')['teambase_count'];
        $total=0;
        $totalsums=$totalsum+$totals;
        foreach ($result as $k => $v) {
            $total+=ceil(($v['totals']/$totals)*($totalsums));
            $result[$k]['totals']=ceil(($v['totals']/$totals)*($totalsums));
            $result[$k]['team_logo'] = "http://sjbimg.qiu7.net/" . $v['team_logo'];
        }
        $result[0]['totals']= $result[0]['totals']+($totalsums-$total);
        return $result;
    }

    /**
     * 冠军排行榜
     * page
     * count
     * need_total
     */
    public static function team($params) {
        self::_checkPageAndCount($params);
        $where = self::_getWhere($params);
        $listdata = usersMysql::name('national_team')
                ->field('team_id,team_name,team_logo,team_group')
                ->select();
        foreach ($listdata as $k => $v) {
            $listdata[$k]['team_logo'] = "http://sjbimg.qiu7.net/" . $v['team_logo'];
        }
        return array("listdata" => $listdata);
    }

    /**
     * 我的推演
     */
    public static function myguess($post) {
        $data = usersMysql::name('guess')
                ->where(array('user_id' => $post['user_id']))
                ->find();
        if($data['guess_first']){
             $team = usersMysql::name('national_team')
                ->where(array('team_id' => $data['guess_first']))
                ->find();
             $data['guess_first_img']=$team['team_logo'];
             $data['guess_first_name']=$team['team_name'];
        }
        return $data;
    }

}
